﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Data;

namespace Dotsa.FAQ.FAQWebPart
{
    public partial class FAQWebPartUserControl : UserControl
    {
        protected Guid ListId;
        protected void Page_Load(object sender, EventArgs e)
        {

            ltError.Visible = false;

            if (!string.IsNullOrEmpty(ListGuid))
            {
                try
                {
                    Guid newId = new Guid(ListGuid);
                    SPList list = SPContext.Current.Web.Lists[newId];
                    DataTable tbItems = list.Items.GetDataTable();
                    rpFAQ.DataSource = tbItems;
                    rpFAQ.DataBind();
                    rpFAQSummary.DataSource = tbItems;
                    rpFAQSummary.DataBind();
                }
                catch (Exception ex)
                {
                    ltError.Visible = true;
                    ltError.Text = ex.Message; 
                }
            }
            else if (!string.IsNullOrEmpty(ListName))
            {
                try
                {
                    SPList list = SPContext.Current.Web.Lists[ListName];
                    ListId = list.ID;
                    DataTable tbItems = list.Items.GetDataTable();
                    rpFAQ.DataSource = tbItems;
                    rpFAQ.DataBind();
                    rpFAQSummary.DataSource = tbItems;
                    rpFAQSummary.DataBind();
                }
                catch (Exception ex)
                {
                    ltError.Visible = true;
                    ltError.Text = ex.Message;
                }
            }
            else
            {
                ltError.Visible = true;
                ltError.Text = "FAQ list is not set";
            }

        }
        public string ListName { get; set; }
        public string ListGuid { get; set; }
    }
}
